package com.dianganjiao.lib.util;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;

/**
 * Copyright：贵州玄机科技有限公司
 * Created by wxl on 2020/8/26 13:55
 * Description：
 * Modify time：
 * Modified by：
 */
public class TabHelper {

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public static void onTabSelectedChangedTextSize(TabLayout.Tab tab, float size, int width, boolean isSelected){
        TextView textView = findTextViewByTab(tab.view);
        ViewGroup.LayoutParams params = tab.view.getLayoutParams();
        if (textView != null) {
            if (isSelected) {
                textView.setScaleX(1.2f);
                textView.setScaleY(1.2f);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            } else {
                textView.setScaleX(1.0f);
                textView.setScaleY(1.0f);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }
            textView.setTextSize(size);
        }
        params.width = DPUtil.dip2px(width);
        tab.parent.setTabGravity(TabLayout.GRAVITY_START);
    }


    private static TextView findTextViewByTab(TabLayout.TabView tabView) {
        try {
            Field field = TabLayout.TabView.class.getDeclaredField("textView");
            field.setAccessible(true);
            TextView textView = (TextView) field.get(tabView);
            return textView;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static int getColor(Context context, int targetColor,int mScrollY,int targetY){
       int color = ContextCompat.getColor(context, targetColor) & 0x00ffffff;
        return ((255 * mScrollY / targetY) << 24) | color;
    }
}
